//
//  BVAddOrderRemarksViewController.m
//  BuyVegetables
//
//  Created by 叶思超 on 15/10/12.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVAddOrderRemarksViewController.h"
#import "BVTextView.h"
#import "BVConst.h"

@interface BVAddOrderRemarksViewController ()<UITextViewDelegate>
/**
 *  输入框
 */
@property (nonatomic, strong) BVTextView *textView;
@end

@implementation BVAddOrderRemarksViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航栏
    [self setupNav];
    
    // 添加输入框
    [self setupTextField];
}

#pragma mark -- 设置导航栏
- (void)setupNav {
    self.title = @"添加备注";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(correctClick)];
}

#pragma mark -- 添加输入框
- (void)setupTextField {
    
    self.textView = [[BVTextView alloc] init];
    [self.view addSubview:self.textView];
    self.textView.layer.borderColor = [BVTextLightGrayColor CGColor];
    self.textView.layer.borderWidth = 1;
    self.textView.layer.masksToBounds = YES;
    self.textView.layer.cornerRadius = 6.0;
    self.textView.placeholder = @"想对卖家说点什么...";
    self.textView.placeholderColor = BVTextLightGrayColor;
    self.textView.frame = CGRectMake(5, 69, BVScreenW-10, 180);
    self.textView.textColor = BVBaseTextBlackColor;
    self.textView.delegate = self;
    self.textView.numLabel.text = @"0/22";
}
#pragma mark -
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    
    long long remainNum ;
    NSString * textContent;
    
    if (self.textView.text.length == 0) {
        self.textView.placeholder = @"想对卖家说点什么...";
        self.textView.numLabel.text = [NSString stringWithFormat:@"0/22"];
    }else if (self.textView.text.length >= 23){
        self.textView.text = [textView.text substringToIndex:22];
        self.textView.placeholder = @"";
    }else{
        self.textView.placeholder = @"";
        textContent = textView.text;
        long long exisetNum = [textContent length];
        remainNum  = 22 - exisetNum;
        self.textView.numLabel.text = [NSString stringWithFormat:@"%lld/%lld",exisetNum,remainNum];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    [textView scrollRangeToVisible:NSMakeRange(textView.text.length, 1)];
    if(range.location >= 23){
        
        return NO;
    }
    return YES;
}

#pragma mark -- 确定
- (void)correctClick {
    if ([self.delegate respondsToSelector:@selector(addOrderRemarks:content:)]) {
        [self.delegate addOrderRemarks:self content:self.textView.text];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -- 返回
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 屏幕触摸事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
@end
